####################################
###     Race and Trust Models    ###
###         Laura Uribe          ###
###      Created: 03/03/2025     ###
###      Updated: 04/15/2025     ###
####################################


# Header -----------------------------------------------------------------------
library(readr)
library(tidyverse)
library(dplyr)
library(stargazer)
library(lme4)
library(data.table)
library(modelsummary)
library(kableExtra)
library(gt)
library(flextable)
library(car)
library(ggplot2)
library(coefplot)
library(ggrepel)
library(ggeffects)
library(marginaleffects)
library(sandwich)
library(multiwayvcov)
library(lmtest)
library(estimatr)
library(lmtest)
library(pandoc)



if(Sys.info()[7] == "laura"){
  setwd("/Users/laura/Dropbox/RA/MIT Evolving Election Administration Landscape/Race, Ethnicity and Political Trust")
}

DF <- fread("2_data/national22.csv")
DF23 <- fread("2_data/national23.csv")
spae <- read_csv("2_data/spae.csv")
spae_2012 <- read_csv("2_data/spae_2012.csv")
spae_2014 <- read_csv("2_data/spae_2014.csv")
spae_2016 <- read_csv("2_data/spae_2016.csv")
spae_2020 <- read_csv("2_data/spae_2020.csv")
spae_2022 <- read_csv("2_data/spae_2022.csv")
spae_sets <- list(spae, spae_2012, spae_2014, spae_2016, spae_2020, spae_2022)



# Setting reference categories + SFE -------------------------------------------

# Ethnorace
DF$ethnorace <- as.factor(DF$ethnorace)
DF$ethnorace <- relevel(DF$ethnorace, ref = "white")

# Party
DF$party <- as.factor(DF$party)
DF$party <- relevel(DF$party, ref = "Independent")

# Household Income
DF$household_collapsed <- as.factor(DF$household_collapsed)
DF$household_collapsed <- relevel(DF$household_collapsed, ref = 1)

# Age
DF$age_cat <- as.factor(DF$age_cat)
DF$age_cat <- relevel(DF$age_cat, ref = 1)

# Education
DF$education_collapsed <- as.factor(DF$education_collapsed)
DF$education_collapsed <- relevel(DF$education_collapsed, ref = 1)


# State - Fixed effects
DF$state_text <- as.factor(DF$state_text)

# SPAE
spae_sets <- lapply(spae_sets, function(DF) {
  DF$ethnorace <- as.factor(DF$ethnorace)
  DF$ethnorace <- relevel(DF$ethnorace, ref = "white")
  DF$party <- as.factor(DF$party)
  DF$party <- relevel(DF$party, ref = "Independent")
  DF$household_collapsed <- as.factor(DF$household_collapsed)
  DF$household_collapsed <- relevel(DF$household_collapsed, ref = 1)
  DF$education_collapsed <- as.factor(DF$education_collapsed)
  DF$education_collapsed <- relevel(DF$education_collapsed, ref = 1) # Age isn't in SPAE
  return(DF)
})

spae$year <- as.factor(spae$year)
spae$year <- relevel(spae$year, ref = "2012")

list2env(setNames(spae_sets, c("spae", "spae_2012", "spae_2014", "spae_2016", "spae_2020", "spae_2022")), envir = .GlobalEnv)


# Table 1 --------------------------------------------------------------
ownstate22_model1 <- lm(ownstatepre ~ ethnorace + party, data = DF)
ownstate22_model2 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female, data = DF) 
ownstate22_model25 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_copartisan, data = DF) 
ownstate22_model26 <- lm(ownstatepre ~ (ethnorace == "black")*logged_vra + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_copartisan, data = DF) 
ownstate22_model27 <- lm(ownstatepre ~ (ethnorace == "black")*democracy_mcmc + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_copartisan, data = DF) 

table_1 <- list("Model 1" = ownstate22_model1,
                "Model 2" = ownstate22_model2,
                "Model 3" = ownstate22_model25,
                "Model 4" = ownstate22_model26,
                "Model 5" = ownstate22_model27)

modelsummary(table_1, output = "4_visuals/Replications/table1_rep.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c("ethnoraceblack" = "Black American",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnoraceindigenous" = "Native American",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other Ethnorace",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index (Grumbach 2022)",
                          "logged_vra" = "Logged Voting Rights Events",
                          "dem_governor" = "Democratic Governor 2022",
                          "state_copartisan" = "Respondent’s Party Wins 2022 Statewide Race",
                          "dem_house" = "Democratic Control of State House 2022",
                          "logged_vra_pre81" = "Pre-1981 Logged Voting Rights Events",
                          "ethnorace == \"black\"TRUE × democracy_mcmc" = "State Democracy Index * Black American",
                          "ethnorace == \"black\"TRUE × logged_vra" = "Logged Voting Rights Events * Black American",
                          "ethnorace == \"black\"TRUE × dem_governor" = "Democratic Governor 2022 * Black American",
                          "ethnorace == \"black\"TRUE × dem_house" = "Democratic Control of State House 2022 * Black American",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


modelsummary(table_1,
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T)


# Model 2 Coefficient Plot -----------------------------------------------------
(model25_coefplot <- 
   coefplot(ownstate22_model25,
            title = "Model 3 Coefficient Plot",
            color = "black",
            decreasing = TRUE,
            factors = c("ethnorace", "party", "household_collapsed", "age_cat", "education_collapsed"),
            predictors = c('ethnorace',
                           'party',
                           'household_collapsed',
                           'age_cat',
                           'education_collapsed',
                           'female'),
            newNames = c('ethnoraceaapi' = 'Asian and Pacific Islander',
                         'ethnoraceindigenous' = 'Native American',
                         'ethnoracelatine' = 'Latine',
                         'ethnoraceother' = 'Other Ethnorace',
                         'ethnoraceblack' = 'Black American',
                         'partyDemocrat' = 'Democrat',
                         'partyRepublican' = 'Republican',
                         "democracy_mcmc" = "State Democracy Index (Grumbach 2022)",
                         "logged_vra" = "Logged Voting Rights Events",
                         "dem_governor" = "Democratic Governor 2022",
                         "dem_house" = "Democratic Control of State House 2022",
                         'household_collapsed2' = '$25,000 to $49,999',
                         'household_collapsed3' = '$50,000 to $74,999',
                         'household_collapsed4' = '$75,000 to $99,999',
                         'household_collapsed5' = '$100,000 to $149,000',
                         'household_collapsed6' = '$150,000 and above',
                         'age_cat2' = '25 to 34',
                         'age_cat3' = '35 to 44',
                         'age_cat4' = '45 to 54',
                         'age_cat5' = '55 to 64',
                         'age_cat6' = '65+',
                         'education_collapsed2' = 'High School Graduate',
                         'education_collapsed3' = 'Some College',
                         'education_collapsed4' = 'College Graduate',
                         'education_collapsed5' = 'Graduate or Professional Degree',
                         'female' = 'Female'
            )) +
   theme(
     plot.title = element_text(size = 16),
     axis.title = element_text(size = 14),
     axis.text = element_text(size = 14),
     legend.title = element_text(size = 14),
     legend.text = element_text(size = 14)
   ))
model25_coefplot
png(filename = "4_visuals/model3_coefplot.png", width = 650, height = 800)

# Figure 3 ---------------------------------------------------------------------
democrats <- spae %>%
  filter(party == "Democrat")
average_data <- democrats %>%
  filter(!is.na(ethnorace)) %>%
  group_by(ethnorace, year) %>%
  summarise(avg_confidence_state = mean(confidence_state, na.rm = TRUE))
(p <- ggplot(average_data, 
             aes(x = year, y = avg_confidence_state, 
                 color = ethnorace, linetype = ethnorace)) +
    geom_line(linewidth = 1) +  # Thicker lines for visibility
    labs(title = "Average Confidence in State Vote by Ethnorace for Democrats Over Time",
         x = "Year",
         y = "Average Confidence in State Vote") +
    scale_color_manual(
      name = "Ethnorace", 
      values = rep("black", 4),  # All lines black (now with clear labels)
      labels = c("Asian", "Black", "Latine", "White")  # Explicit label order
    ) +
    scale_linetype_manual(
      name = "Ethnorace", 
      values = c("dotdash", "dashed", "dotted", "solid"),  # Same as before
      labels = c("Asian", "Black", "Latine", "White")  # Match color scale
    ) +
    theme_minimal() +
    ylim(2.5, 4) +
    scale_x_continuous(breaks = c(2012, 2014, 2016, 2018, 2020, 2022)) +
    theme(
      plot.title = element_text(size = 16, face = "bold"),
      axis.title = element_text(size = 16),
      axis.text = element_text(size = 16),
      legend.title = element_text(size = 16),
      legend.text = element_text(size = 16)
    ))
ggsave("4_visuals/SPAE/Average Confidence in State Vote by Ethnorace for Democrats Over Time.png", p, width = 13, height = 7)

# Figure 4 ---------------------------------------------------------------------
republicans <- spae %>%
  filter(party == "Republican")
average_data <- republicans %>%
  filter(!is.na(ethnorace)) %>%
  group_by(ethnorace, year) %>%
  summarise(avg_confidence_state = mean(confidence_state, na.rm = TRUE))
(p <- ggplot(average_data, 
             aes(x = year, y = avg_confidence_state, 
                 color = ethnorace, linetype = ethnorace)) +
    geom_line(linewidth = 1) +  # Thicker lines for visibility
    labs(title = "Average Confidence in State Vote by Ethnorace for Republicans Over Time",
         x = "Year",
         y = "Average Confidence in State Vote") +
    scale_color_manual(
      name = "Ethnorace", 
      values = rep("black", 4),  # All lines black (now with clear labels)
      labels = c("Asian", "Black", "Latine", "White")  # Explicit label order
    ) +
    scale_linetype_manual(
      name = "Ethnorace", 
      values = c("dotdash", "dashed", "dotted", "solid"),  # Same as before
      labels = c("Asian", "Black", "Latine", "White")  # Match color scale
    ) +
    theme_minimal() +
    ylim(2.5, 4) +
    scale_x_continuous(breaks = c(2012, 2014, 2016, 2018, 2020, 2022)) +
    theme(
      plot.title = element_text(size = 16, face = "bold"),
      axis.title = element_text(size = 16),
      axis.text = element_text(size = 16),
      legend.title = element_text(size = 16),
      legend.text = element_text(size = 16)
    ))
ggsave("4_visuals/SPAE/Average Confidence in State Vote by Ethnorace for Republicans Over Time.png", p, width = 13, height = 7)

# Table 2 ----------------------------------------------------------------------

# 2022 Datasets
DF <- fread("2_data/national22.csv")
CO <- fread("2_data/colorado22.csv")
GA <- fread("2_data/georgia22.csv")
LA <- fread("2_data/la22.csv")
TX <- fread("2_data/texas22.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

# Reference Categories
# Ethnorace
DF$ethnorace <- as.factor(DF$ethnorace)
DF$ethnorace <- relevel(DF$ethnorace, ref = "white")

# Party
DF$party <- as.factor(DF$party)
DF$party <- relevel(DF$party, ref = "Independent")

# Household Income
DF$household_collapsed <- as.factor(DF$household_collapsed)
DF$household_collapsed <- relevel(DF$household_collapsed, ref = 1)

# Age
DF$age_cat <- as.factor(DF$age_cat)
DF$age_cat <- relevel(DF$age_cat, ref = 1)

# Education
DF$education_collapsed <- as.factor(DF$education_collapsed)
DF$education_collapsed <- relevel(DF$education_collapsed, ref = 1)

# Subsets by Ethnorace
white22 <- subset(DF, DF$ethnorace == "white")
black22 <- subset(DF, DF$ethnorace == "black")
indigenous22 <- subset(DF, DF$ethnorace == "indigenous")
aapi22 <- subset(DF, DF$ethnorace == "aapi")
latine22 <- subset(DF, DF$ethnorace == "latine")
other_er22 <- subset(DF, DF$ethnorace == "other")

m.full <- lm(otherstates ~ d_treatment, DF)
m.w <- lm(otherstates ~ d_treatment, white22)
m.a <- lm(otherstates ~ d_treatment, aapi22)
m.b <- lm(otherstates ~ d_treatment, black22)
m.l <- lm(otherstates ~ d_treatment, latine22)
m.n <- lm(otherstates ~ d_treatment, indigenous22)
m.ethnorace <- lm(otherstates ~ d_treatment*ethnorace, DF)

binary_models <- list("Full Sample" = m.full,
                      "Subsample: White" = m.w,
                      "Subsample: AAPI" = m.a,
                      "Subsample: Black" = m.b, 
                      "Subsample: Latine" = m.l,
                      "Subsample: Native American" = m.n,
                      "Full Sample, Interacted" = m.ethnorace)

modelsummary(binary_models, output = "4_visuals/Experimental Results/experiment_ethnorace.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceindigenous" = "Indigenous",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceother" = "Other",
                          "ethnoracewhite"="White",
                          "d_treatment:ethnoraceaapi" = "Treated X AAPI",
                          "d_treatment:ethnoraceindigenous" = "Treated X Indigenous",
                          "d_treatment:ethnoracelatine" = "Treated X Latine",
                          "d_treatment:ethnoraceother" = "Treated X Other",
                          "d_treatment:ethnoracewhite" = "Treated X White"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


# Appendix Models --------------------------------------------------------------
# Re-run header
DF <- fread("2_data/national22.csv")
# Ethnorace
DF$ethnorace <- as.factor(DF$ethnorace)
DF$ethnorace <- relevel(DF$ethnorace, ref = "white")

# Party
DF$party <- as.factor(DF$party)
DF$party <- relevel(DF$party, ref = "Independent")

# Household Income
DF$household_collapsed <- as.factor(DF$household_collapsed)
DF$household_collapsed <- relevel(DF$household_collapsed, ref = 1)

# Age
DF$age_cat <- as.factor(DF$age_cat)
DF$age_cat <- relevel(DF$age_cat, ref = 1)

# Education
DF$education_collapsed <- as.factor(DF$education_collapsed)
DF$education_collapsed <- relevel(DF$education_collapsed, ref = 1)

# A.2 --------------------------------------------------------------------------

ownstate22_model1sfe <- lm(ownstatepre ~ ethnorace + party + state_text, data = DF)
ownstate22_model3 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + state_text, data = DF) 
ownstate22_model22 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_text, data = DF) 
ownstate22_model23 <- lm(ownstatepre ~ (ethnorace == "black")*logged_vra + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_text, data = DF) 
ownstate22_model24 <- lm(ownstatepre ~ (ethnorace == "black")*democracy_mcmc + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + state_text, data = DF) 

sfe_models <- list("Model 1" = ownstate22_model1sfe,
                   "Model 2" = ownstate22_model3,
                   "Model 3" = ownstate22_model22,
                   "Model 4" = ownstate22_model23,
                   "Model 5" = ownstate22_model24)

modelsummary(sfe_models, output = "4_visuals/Replications/ownstate_sfe_text_logged.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c("ethnoraceblack" = "Black American",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnoraceindigenous" = "Indigenous American",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other Ethnorace",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index (Grumbach 2022)",
                          "logged_vra" = "Logged Voting Rights Events",
                          "dem_governor" = "Democratic Governor 2022",
                          "dem_house" = "Democratic Control of State House 2022",
                          "logged_vra_pre81" = "Pre-1981 Logged Voting Rights Events",
                          "ethnorace == \"black\"TRUE × democracy_mcmc" = "State Democracy Index * Black American",
                          "ethnorace == \"black\"TRUE × logged_vra" = "Logged Voting Rights Events * Black American",
                          "ethnorace == \"black\"TRUE × dem_governor" = "Democratic Governor 2022 * Black American",
                          "ethnorace == \"black\"TRUE × dem_house" = "Democratic Control of State House 2022 * Black American",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

modelsummary(sfe_models,
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T)

# A.3 --------------------------------------------------------------------------

ownstate22_model1 <- lm(ownstatepre ~ ethnorace + party, data = DF)
ownstate22_model2 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female, data = DF) 
ownstate22_model13nsfe <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + percapita_1k_vra, data = DF)
ownstate22_model12nsfe <- lm(ownstatepre ~ (ethnorace == "black")*democracy_mcmc + ethnorace + party + household_collapsed + age_cat + education_collapsed + female, data = DF) 
ownstate22_model14nsfe <- lm(ownstatepre ~ (ethnorace == "black")*percapita_1k_vra + ethnorace + party + household_collapsed + age_cat + education_collapsed + female, data = DF) 

full_models <- list("Model 1" = ownstate22_model1,
                    "Model 2" = ownstate22_model2,
                    "Model 3" = ownstate22_model13nsfe,
                    "Model 4" = ownstate22_model12nsfe,
                    "Model 5" = ownstate22_model14nsfe)

modelsummary(full_models, output = "4_visuals/Replications/ownstate_text_per1kcap_nsfe_full.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "ethnoraceblack" = "Black American",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceindigenous" = "Indigenous American",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other Ethnorace",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "logged_vra" = "Logged Voting Rights Events",
                          "dem_governor" = "Democratic Governor 2022",
                          "dem_house" = "Democratic Control of State House 2022",
                          "logged_vra_pre81" = "Pre-1981 Logged Voting Rights Events",
                          "ethnorace == \"black\"TRUE × democracy_mcmc" = "State Democracy Index * Black American",
                          "ethnorace == \"black\"TRUE × logged_vra" = "Logged Voting Rights Events * Black American",
                          "ethnorace == \"black\"TRUE × dem_governor" = "Democratic Governor 2022 * Black American",
                          "ethnorace == \"black\"TRUE × dem_house" = "Democratic Control of State House 2022 * Black American",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

# A.4 --------------------------------------------------------------------------

ownstate22_model1sfe <- lm(ownstatepre ~ ethnorace + party + state_text, data = DF)
ownstate22_model3 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + state_text, data = DF) 
ownstate22_model11 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + percapita_vra + state_text, data = DF) 
ownstate22_model36 <- lm(ownstatepre ~ (ethnorace == "black")*pc_vra_black + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model37 <- lm(ownstatepre ~ (ethnorace == "latine")*pc_vra_latine + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model38 <- lm(ownstatepre ~ (ethnorace == "aapi")*pc_vra_aapi + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model39 <- lm(ownstatepre ~ (ethnorace == "indigenous")*pc_vra_native + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 


full_models <- list("Model 1" = ownstate22_model1sfe,
                    "Model 2" = ownstate22_model3,
                    "Model 3" = ownstate22_model11,
                    "Model 4" = ownstate22_model36,
                    "Model 5" = ownstate22_model37,
                    "Model 6" = ownstate22_model39,
                    "Model 7" = ownstate22_model38)

modelsummary(full_models, output = "4_visuals/Replications/ownstate_vra_pc_byethno_text.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "ethnoraceblack" = "Black",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnorace == \"latine\"TRUE" = "Latine",
                          "ethnorace == \"indigenous\"TRUE" = "Native American",
                          "ethnorace == \"aapi\"TRUE" = "Asian and Pacific Islander",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceindigenous" = "Native American",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index",
                          "dem_governor" = "Democratic Governor",
                          "pc_vra_black" = "Per Capita Voting Rights Events Against Black Americans",
                          "pc_vra_latine" = "Per Capita Voting Rights Events Against Latine Americans",
                          "pc_vra_native" = "Per Capita Voting Rights Events Against Native Americans",
                          "pc_vra_aapi" = "Per Capita Voting Rights Events Against Asian and Pacific Islander Americans",
                          "d_treatment:ethnoraceaapi" = "Treated X AAPI",
                          "d_treatment:ethnoraceindigenous" = "Treated X Indigenous",
                          "d_treatment:ethnoracelatine" = "Treated X Latine",
                          "d_treatment:ethnoraceother" = "Treated X Other",
                          "d_treatment:ethnoracewhite" = "Treated X White",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

# A.5 --------------------------------------------------------------------------

ownstate22_model1sfe <- lm(ownstatepre ~ ethnorace + party + state_text, data = DF)
ownstate22_model3 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + state_text, data = DF) 
ownstate22_model11 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + percapita_vra + state_text, data = DF) 
ownstate22_model40 <- lm(ownstatepre ~ (ethnorace == "black")*leg_pc_black + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model41 <- lm(ownstatepre ~ (ethnorace == "latine")*leg_pc_latine + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model42 <- lm(ownstatepre ~ (ethnorace == "aapi")*leg_pc_aapi + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 
ownstate22_model43 <- lm(ownstatepre ~ (ethnorace == "indigenous")*leg_pc_native + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + dem_governor, data = DF) 


full_models <- list("Model 1" = ownstate22_model1sfe,
                    "Model 2" = ownstate22_model3,
                    "Model 3" = ownstate22_model11,
                    "Model 4" = ownstate22_model40,
                    "Model 5" = ownstate22_model41,
                    "Model 6" = ownstate22_model43,
                    "Model 7" = ownstate22_model42)

modelsummary(full_models, output = "4_visuals/Replications/ownstate_descrep_text.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "ethnoraceblack" = "Black",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnorace == \"latine\"TRUE" = "Latine",
                          "ethnorace == \"indigenous\"TRUE" = "Native American",
                          "ethnorace == \"aapi\"TRUE" = "Asian and Pacific Islander",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceindigenous" = "Native American",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index",
                          "dem_governor" = "Democratic Governor",
                          "leg_pc_black" = "Black Legislative Diversity",
                          "leg_pc_latine" = "Latine Legislative Diversity",
                          "leg_pc_native" = "Native American Legislative Diversity",
                          "leg_pc_aapi" = "Asian and Pacific Islander Legislative Diversity",
                          "pc_vra_black" = "Per Capita Voting Rights Events Against Black Americans",
                          "pc_vra_latine" = "Per Capita Voting Rights Events Against Latine Americans",
                          "pc_vra_native" = "Per Capita Voting Rights Events Against Native Americans",
                          "pc_vra_aapi" = "Per Capita Voting Rights Events Against Asian and Pacific Islander Americans",
                          "d_treatment:ethnoraceaapi" = "Treated X AAPI",
                          "d_treatment:ethnoraceindigenous" = "Treated X Indigenous",
                          "d_treatment:ethnoracelatine" = "Treated X Latine",
                          "d_treatment:ethnoraceother" = "Treated X Other",
                          "d_treatment:ethnoracewhite" = "Treated X White",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

# A.6 --------------------------------------------------------------------------
# Pooling experimental subsets
DF <- fread("2_data/national22.csv")
CO <- fread("2_data/colorado22.csv")
GA <- fread("2_data/georgia22.csv")
LA <- fread("2_data/la22.csv")
TX <- fread("2_data/texas22.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

# Setting reference categories

# Ethnorace
DF$ethnorace <- as.factor(DF$ethnorace)
DF$ethnorace <- relevel(DF$ethnorace, ref = "white")

# Party
DF$party <- as.factor(DF$party)
DF$party <- relevel(DF$party, ref = "Independent")

# Household Income
DF$household_collapsed <- as.factor(DF$household_collapsed)
DF$household_collapsed <- relevel(DF$household_collapsed, ref = 1)

# Age
DF$age_cat <- as.factor(DF$age_cat)
DF$age_cat <- relevel(DF$age_cat, ref = 1)

# Education
DF$education_collapsed <- as.factor(DF$education_collapsed)
DF$education_collapsed <- relevel(DF$education_collapsed, ref = 1)

# Minority Trust Groups
DF$minority_trust <- ifelse(DF$ethnorace == "white" | DF$ethnorace == "aapi", 0, 1)

# DVs
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre

m1.1 <- lm(ownstate ~ d_treatment, DF)
m1.2 <- lm(otherstates ~ d_treatment, DF)
m1.3 <- lm(ownstate ~ d_treatment*minority_trust, DF)
m1.4 <- lm(otherstates ~ d_treatment*minority_trust, DF)

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Trust Own State" = m1.3,
                      "Trust Other States" = m1.4)
modelsummary(binary_models, output = "4_visuals/Replications/binary22_original_minoritygroup.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          'minority_trust' = 'Politically Excluded Ethnoracial Groups',
                          'd_treatment:minority_trust' = 'Treated X Politically Excluded Ethnoracial Groups'),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

rm(list = ls())

# A.7 --------------------------------------------------------------------------
# National DF and setting reference categories
DF <- fread("2_data/national22.csv")

# Dummy outcomes
DF$d_ownstatepre <- ifelse(DF$ownstatepre >= 3, 1, 0)
DF$d_vote22 <- ifelse(DF$Voted.in.2022 == "Yes, I voted in this election", 1, 0)

# Subsets by ethnorace
white22 <- subset(DF, DF$ethnorace == "white")
black22 <- subset(DF, DF$ethnorace == "black")
indigenous22 <- subset(DF, DF$ethnorace == "indigenous")
aapi22 <- subset(DF, DF$ethnorace == "aapi")
latine22 <- subset(DF, DF$ethnorace == "latine")
other_er22 <- subset(DF, DF$ethnorace == "other")

# Correlations
(cor1 <- cor.test(DF$d_ownstatepre, DF$d_vote22, method = c("pearson")))
(cor1_white <- cor.test(white22$d_ownstatepre, white22$d_vote22, method = c("pearson")))
(cor1_black <- cor.test(black22$d_ownstatepre, black22$d_vote22, method = c("pearson")))
(cor1_indigenous <- cor.test(indigenous22$d_ownstatepre, indigenous22$d_vote22, method = c("pearson")))
(cor1_aapi <- cor.test(aapi22$d_ownstatepre, aapi22$d_vote22, method = c("pearson")))
(cor1_latine <- cor.test(latine22$d_ownstatepre, latine22$d_vote22, method = c("pearson")))

# A.8 --------------------------------------------------------------------------

DF$stateVRA <- ifelse(DF$state_text == "California" | DF$state_text == "Oregon" | DF$state_text == "Washington" | 
                        DF$state_text == "New York" | DF$state_text == "Virginia", 
                      1, 0)

ownstate22_1vrafe <- lm(ownstatepre ~ ethnorace + party + stateVRA, data = DF)
ownstate22_2vrafe <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + stateVRA, data = DF) 
ownstate22_3vrafe <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + stateVRA, data = DF) 
ownstate22_4vrafe <- lm(ownstatepre ~ (ethnorace == "black")*logged_vra + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + stateVRA, data = DF) 
ownstate22_5vrafe <- lm(ownstatepre ~ (ethnorace == "black")*democracy_mcmc + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor + stateVRA, data = DF) 

table_vrafe <- list("Model 1" = ownstate22_1vrafe,
                "Model 2" = ownstate22_2vrafe,
                "Model 3" = ownstate22_3vrafe,
                "Model 4" = ownstate22_4vrafe,
                "Model 5" = ownstate22_5vrafe)

modelsummary(table_vrafe, output = "4_visuals/Replications/a8_vrafe_rep.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c("ethnoraceblack" = "Black American",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnoraceindigenous" = "Native American",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other Ethnorace",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index (Grumbach 2022)",
                          "logged_vra" = "Logged Voting Rights Events",
                          "dem_governor" = "Democratic Governor 2022",
                          "dem_house" = "Democratic Control of State House 2022",
                          "logged_vra_pre81" = "Pre-1981 Logged Voting Rights Events",
                          "ethnorace == \"black\"TRUE × democracy_mcmc" = "State Democracy Index * Black American",
                          "ethnorace == \"black\"TRUE × logged_vra" = "Logged Voting Rights Events * Black American",
                          "ethnorace == \"black\"TRUE × dem_governor" = "Democratic Governor 2022 * Black American",
                          "ethnorace == \"black\"TRUE × dem_house" = "Democratic Control of State House 2022 * Black American",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


modelsummary(table_vrafe, output = "4_visuals/Visuals Drafts/a8_vrafe_rep.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T)

# Standard Errors Clustered at State -------------------------------------------

ownstate22_model44 <- lm(ownstatepre ~ ethnorace + party, data = DF)
ownstate22_model45 <- lm(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female, data = DF) 
ownstate22_model46 <- lm_robust(ownstatepre ~ ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor, clusters = state_text, data = DF) 
ownstate22_model47 <- lm_robust(ownstatepre ~ (ethnorace == "black")*logged_vra + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor, clusters = state_text, data = DF) 
ownstate22_model48 <- lm_robust(ownstatepre ~ (ethnorace == "black")*democracy_mcmc + ethnorace + party + household_collapsed + age_cat + education_collapsed + female + democracy_mcmc + logged_vra + dem_governor, clusters = state_text, data = DF)

secluster <- list("Model 1" = ownstate22_model44,
                "Model 2" = ownstate22_model45,
                "Model 3" = ownstate22_model46,
                "Model 4" = ownstate22_model47,
                "Model 5" = ownstate22_model48)

modelsummary(secluster, output = "4_visuals/Replications/standard_error_state_cluster.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c("ethnoraceblack" = "Black American",
                          "ethnorace == \"black\"TRUE" = "Black",
                          "ethnoraceindigenous" = "Native American",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "ethnoraceother" = "Other Ethnorace",
                          "ethnoracewhite" = "White",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "democracy_mcmc" = "State Democracy Index (Grumbach 2022)",
                          "logged_vra" = "Logged Voting Rights Events",
                          "dem_governor" = "Democratic Governor 2022",
                          "dem_house" = "Democratic Control of State House 2022",
                          "logged_vra_pre81" = "Pre-1981 Logged Voting Rights Events",
                          "ethnorace == \"black\"TRUE × democracy_mcmc" = "State Democracy Index * Black American",
                          "ethnorace == \"black\"TRUE × logged_vra" = "Logged Voting Rights Events * Black American",
                          "ethnorace == \"black\"TRUE × dem_governor" = "Democratic Governor 2022 * Black American",
                          "ethnorace == \"black\"TRUE × dem_house" = "Democratic Control of State House 2022 * Black American",
                          "household_collapsed2" = "$25,000 to $49,999",
                          "household_collapsed3" = "$50,000 to $74,999",
                          "household_collapsed4" = "$75,000 to $99,999",
                          "household_collapsed5" = "$100,000 to $149,000",
                          "household_collapsed6" = "$150,000 and above",
                          "age_cat2" = "25 to 34",
                          "age_cat3" = "35 to 44",
                          "age_cat4" = "45 to 54",
                          "age_cat5" = "55 to 64",
                          "age_cat6" = "65+",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


modelsummary(secluster,
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T)

###
rm(list = ls())
# SPAE Regression --------------------------------------------------------------
spae12_model1 <- lm(confidence_state ~ ethnorace + party, data = spae_2012)
spae12_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female, data = spae_2012) 
spae12_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience + obama, data = spae_2012)

spae14_model1 <- lm(confidence_state ~ ethnorace + party, data = spae_2014)
spae14_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female, data = spae_2014) 
spae14_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience, data = spae_2014)

spae16_model1 <- lm(confidence_state ~ ethnorace + party, data = spae_2016)
spae16_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female, data = spae_2016) 
spae16_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience, data = spae_2016)

spae20_model1 <- lm(confidence_state ~ ethnorace + party, data = spae_2020)
spae20_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female, data = spae_2020) 
spae20_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience, data = spae_2020)

spae22_model1 <- lm(confidence_state ~ ethnorace + party, data = spae_2022)
spae22_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female, data = spae_2022) 
spae22_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience, data = spae_2022)

spae_model1 <- lm(confidence_state ~ ethnorace + party + year, data = spae)
spae_model2 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + year, data = spae) 
spae_model3 <- lm(confidence_state ~ ethnorace + party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience + year, data = spae) 


spae_table <- list("SPAE 12" = spae12_model1,
                   "SPAE 12" = spae12_model2,
                   "SPAE 14" = spae14_model1,
                   "SPAE 14" = spae14_model2,
                   "SPAE 16" = spae16_model1,
                   "SPAE 16" = spae16_model2,
                   "SPAE 20" = spae20_model1,
                   "SPAE 20" = spae20_model2,
                   "SPAE 22" = spae22_model1,
                   "SPAE 22" = spae22_model2,
                   "SPAE Full" = spae_model1,
                   "SPAE Full" = spae_model2)

spae_table_1half <- list("SPAE 12" = spae12_model1,
                         "SPAE 12" = spae12_model2,
                         "SPAE 12" = spae12_model3,
                         "SPAE 14" = spae14_model1,
                         "SPAE 14" = spae14_model2,
                         "SPAE 14" = spae14_model3,
                         "SPAE 16" = spae16_model1,
                         "SPAE 16" = spae16_model2,
                         "SPAE 16" = spae16_model3)

spae_table_2half <- list("SPAE 20" = spae20_model1,
                         "SPAE 20" = spae20_model2,
                         "SPAE 20" = spae20_model3,
                         "SPAE 22" = spae22_model1,
                         "SPAE 22" = spae22_model2,
                         "SPAE 22" = spae22_model3,
                         "SPAE Full" = spae_model1,
                         "SPAE Full" = spae_model2,
                         "SPAE Full" = spae_model3)


modelsummary(spae_table_1half, output = "4_visuals/spae_table_1half.html",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

modelsummary(spae_table_2half, output = "4_visuals/spae_table_2half.html",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

modelsummary(spae_table, output = "4_visuals/spae_table.html",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')

modelsummary(spae_table, output = "4_visuals/spae_table_text.html",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c("ethnoraceblack" = "Black",
                          "ethnoracelatine" = "Latine",
                          "ethnoraceaapi" = "Asian and Pacific Islander",
                          "partyDemocrat" = "Democrat",
                          "partyRepublican" = "Republican",
                          "household_collapsed2" = "Est. $25,000 to $49,999",
                          "household_collapsed3" = "Est. $50,000 to $74,999",
                          "household_collapsed4" = "Est. $75,000 to $99,999",
                          "household_collapsed5" = "Est. $100,000 to $149,000",
                          "household_collapsed6" = "Est. $150,000 and above",
                          "education_collapsed2" = "High School Graduate",
                          "education_collapsed3" = "Some College",
                          "education_collapsed4" = "College Graduate",
                          "education_collapsed5" = "Graduate or Professional Degree",
                          "female" = "Female"),
             coef_omit = "Intercept",
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')


modelsummary(table_1,
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T)


cor(spae$confidence_usa, spae$confidence_state, use = "complete.obs") # 0.62
cor(spae$confidence_usa, spae$confidence_self, use = "complete.obs") # 0.46
cor(spae$confidence_state, spae$confidence_self, use = "complete.obs") # 0.65

# SPAE 2012 Black Subset -------------------------------------------------------
spae_black12 <- subset(spae, ethnorace == "black" & year == 2012)

spae12_black <- lm(confidence_state ~ party + household_collapsed + education_collapsed + female + democracy_mcmc + logged_vra + voter_experience + obama, data = spae_black12)
summary(spae12_black)
# Descriptive statistics on IVs and DV -----------------------------------------
summary(DF$ownstatepre)
summary(DF$ethnorace)
summary(DF$party)
summary(DF$democracy_mcmc)
summary(DF$logged_vra)
summary(DF$dem_governor)
summary(DF$state_copartisan)
summary(DF$household_collapsed)
summary(DF$age_cat)
summary(DF$education_collapsed)
summary(DF$female)

sd(DF$ownstatepre, na.rm = TRUE)
sd(DF$democracy_mcmc)
sd(DF$logged_vra)
sd(DF$dem_governor)
sd(DF$state_copartisan)
sd(DF$household_collapsed, na.rm = TRUE)
sd(DF$age_cat)
sd(DF$education_collapsed, na.rm = TRUE)
sd(DF$female)

hist(DF$ownstatepre, 
     breaks = seq(0.5, 4.5, by = 1),
     main = "Histogram of Trust Own State",
     xlab = "Trust Level", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 1200),
     na.rm = TRUE)  

hist(DF$democracy_mcmc, 
     main = "Histogram of Grumbach's State Democracy Index",
     xlab = "Democracy Index", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 600),
     na.rm = TRUE)

hist(DF$logged_vra, 
     main = "Histogram of Logged Voting Rights Incidences",
     xlab = "Voting Rights Incidences", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 600),
     na.rm = TRUE)

hist(DF$household_collapsed, 
     breaks = seq(0.5, 6.5, by = 1),
     main = "Histogram of Income",
     xlab = "Income Levels", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 1000),
     na.rm = TRUE)

hist(DF$age_cat, 
     breaks = seq(0.5, 6.5, by = 1),
     main = "Histogram of Age Levels",
     xlab = "Age Category Levels", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 800),
     na.rm = TRUE)

hist(DF$education_collapsed, 
     breaks = seq(0.5, 5.5, by = 1),
     main = "Histogram of Education Levels",
     xlab = "Education Levels", 
     ylab = "Frequency",
     border = "black",
     ylim = c(0, 1000),
     na.rm = TRUE)


